﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;


namespace LibrarySystem
{
    public partial class Form3 : Form
    {
        SqlConnection Connection_Main = new SqlConnection();
        string str;
        public Form3()
        {
            InitializeComponent();
            str = "integrated security=SSPI;" + "data source='.';" + "initial catalog='alssafa'";
            Connection_Main.ConnectionString = str;
            Connection_Main.Open();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label3.Text = DateTime.Now.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F1 = new Form1();
            F1.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((comboBox1.Text == "") || (username1.Text == ""))
            {
                if (username1.Text == "")
                    lblmsg.Text = "من فضل ادخل إسم الكتاب";
                else if (comboBox1.Text == "")
                    lblmsg.Text = "من فضلك اختر الصنف";
            }
            else if((comboBox1.Text == "") && (username1.Text == ""))
            {
                lblmsg.Text = "من فضل ادخل إسم الكتاب واختر الصنف";
            }
            else
            {
                SqlDataAdapter Search_Book_Adapter = new SqlDataAdapter();
                SqlCommand Search_Book_Command = new SqlCommand();
                DataSet Search_Book_Ds = new DataSet();
                Search_Book_Command.CommandType = CommandType.Text;
                Search_Book_Command.Connection = Connection_Main;
                dataGridView1.ClearSelection();
                Search_Book_Command.CommandText = "Select كود_الكتاب ,إسم_الكتاب,إسم_المؤلف,سعر_الكتاب,التصنيف from BOOKS where إسم_الكتاب = '" + username1.Text + "' AND التصنيف = '" + comboBox1.Text + "' OR إسم_المؤلف = '" + password1.Text + "'";
                Search_Book_Adapter.SelectCommand = Search_Book_Command;
                Search_Book_Adapter.Fill(Search_Book_Ds, "BOOKS");
                dataGridView1.DataSource = Search_Book_Ds;
                dataGridView1.DataMember = "BOOKS";
                if(dataGridView1.RowCount <= 1)
                {
                    lblmsg.Text = "لا يوجد كتاب بهذا الإسم!";
                }
                else
                {
                    lblmsg.Text = "";
                }
            }
        }
    }
}
